package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

//处理mybatis核心配置类
public class MybatisConfig {

    //创建一个方法获取SqlSessionFactoryBean对象
    // 隐藏一个@Autowired
    @Bean//将方法获取的对象存入IOC容器
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Qualifier("dataSource1")DataSource dataSource){
            //调用ioc容器对象
        SqlSessionFactoryBean sb = new SqlSessionFactoryBean();
            sb.setDataSource(dataSource);
            //起别名
            sb.setTypeAliasesPackage("com.itheima.com.itheima.domain");
            //将容器返回
            return sb;
    }

    //创建一个方法获得扫描器器对象
    @Bean//将方法获取的对象存入IOC容器
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer mc = new MapperScannerConfigurer();
        mc.setBasePackage("com.itheima.com.itheima.mapper");//扫描mapper包下的所有类和接口
        return mc;
    }

}
